<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Env;
use think\facade\Session;
use app\facade\Vpsx;
use think\facade\Validate;
class Psx extends Base
{
    public function lookpsx(){
        $request=new request();
        $pid=$request->get('id');
        $grade=$request->get('grade');
        if($grade==0){
            $this->error('该商品还没有添加属性等级','product/index');
        }else{
            $data=Db::table('psx')->where('pid',$pid)->paginate(10);
        }
        $this->assign('data',$data);
        return $this->fetch('index');
    }
    public function search()
    {
        $request=new request();
        $sname=$request->post('sname');
        if(empty($sname)){
            $data=Db::table('psx')->paginate(10);
        }else{
            $data=Db::table('psx')->where('pname','like','%'.$sname.'%')->paginate(10);
        }
        $this->assign('data',$data);
        return $this->fetch('index');
    }
    public function index()
    {
        $data=Db::table('psx')->paginate(10);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function lookpage(){
        $request=new request();
        $psid= intval($request->get("id"));
        $data=Db::name('psx')->where('psid',$psidid)->find();       
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 进入
    public function addpage(){
        $request=new request();
        $pid = $request->get("pid");
        $pname = $request->get("pname");
        $this->assign('pid',$pid);
        $this->assign('pname',$pname);
        return $this->fetch();
    }
    // 添加
    public  function addad(){
        // $adxm1=Session::get('adxm');
        $request=new request();
        $pid = $request->post("pid");
        $pname = $request->post("pname");
        $psname = $request->post("psname");
        $psprice = $request->post("psprice");
        $allnum = $request->post("allnum");
        $data1=[];
        for($i=0;$i<count($psname);$i++){
            $data2=[];
            $data2['pid']=$pid;
            $data2['pname']=$pname;
            $data2['psname']=$psname[$i];
            $data2['psprice']=$psprice[$i];
            $data2['allnum']=$allnum[$i];
            $checkdata=[
                "psname"=>$psname[$i],
                "psprice"=>$psprice[$i]
                ];
            if(!Vpsx::check($checkdata)){
    			return Vpsx::getError();
    		}else{
                $data1[$i]=$data2;
    		}
        }
        
        $re=Db::name('psx')->insertAll($data1);
        if($re){
            $this->success('新增属性等级成功', 'psx/index?pid='.$pid);
        }else{
            return $this->error('新增属性等级失败');
        }
            
            
        // }else{
        //     // return $this->error('用户名已存在，请重新输入！');
        //     var_dump($data);
        // }
    }
    // 进入更改
    public function editpage(){
        $request=new request();
        $pid= intval($request->get("id"));
        $data=Db::name('psx')->where('pid',$pid)->select();       
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 更改属性
    public  function editad(){
        $request=new request();
        $pid=$request->post('pid');
        $pname = $request->post("pname");
        $psname = $request->post("psname");
        $psprice = $request->post("psprice");
        $allnum = $request->post("allnum");
        $data1=[];
        $da=Db::name('psx')->where('pid',$pid)->delete();
        if($da){
            for($i=0;$i<count($psname);$i++){
                if(!empty($psname[$i])&&!empty($psprice[$i])){
                    $data2=[];
                    $data2['pid']=$pid;
                    $data2['pname']=$pname;
                    $data2['psname']=$psname[$i];
                    $data2['psprice']=$psprice[$i];
                    $data2['allnum']=$allnum[$i];
                    $data1[$i]=$data2;
                }
            }
        }
        $re=Db::name('psx')->insertAll($data1);
        if($re){
            $this->success('修改属性等级成功', 'psx/index?pid='.$pid);
        }else{
            return $this->error('修改属性等级失败');
        }

    }
    // 删除属性
    public function delpsx()
    {
        $request=new request();
        $psid=$request->get("id");
        $pid=Db::name('psx')->field('pid')->where('psid',$psid)->find();
        $da=Db::name('psx')->delete($psid);
        if($da){
            // dump($pid);
            $num=Db::name('psx')->where('pid',$pid['pid'])->count();
            // 属性删除完后要把商品的grade改为零
            if($num==0){
                $d=Db::name('product')->where('pid',$pid['pid'])->update(['grade'=>0]);
                if($d){
                    $this->success('删除成功', 'psx/index');
                }
            }else{
                $this->success('删除成功', 'psx/index');
            }
            
        }
        else{
            return $this->error('删除失败');
        }
    }




}
